package com.lottery.ruok.lottrey.ui.component;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.LiuhecaiPrizeActivity;
import com.lottery.ruok.lottrey.activity.PrizeListActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;
import com.nex3z.flowlayout.FlowLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import glideHelp.GlideHelper;
import ui.AreaCircle;
import utils.Utils;

/**
 * 开奖ItemView
 */
public class PrizeItemView extends BaseRelativeLayout {

    @BindView(R.id.container)
    FlowLayout container;
    @BindView(R.id.logo)
    ImageView logo;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.tip)
    TextView tip;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.play)
    RelativeLayout play;

    public PrizeItemView(Context context) {
        super(context);
    }

    public PrizeItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PrizeItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        LayoutInflater.from(mContext).inflate(R.layout.prize_category_item, this);
        ButterKnife.bind(this, PrizeItemView.this);
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(final GetWholeCategoryResponse.ItemsBean data) {
        GlideHelper.loadImage(mContext, data.getImage_url(), logo);
        name.setText(data.getName());
        if (data.getPrize_num() != null && data.getPrize_num().contains(",")) {
            String[] temp = data.getPrize_num().split(",");
            number.setVisibility(View.GONE);
            container.setVisibility(View.GONE);
            container.removeAllViews();

            if (data.getCode().equals("xglhc") || data.getCode().equals("jslhc")|| data.getCode().equals("js10flhc")) {
                if (Common.xglhcColor != null) {
                    for (int t = 0; t < temp.length; t++) {
                        TextView areaCircle = new TextView(mContext);
                        if (Common.xglhcColor.getRed().contains(temp[t])) {
                            areaCircle.setBackgroundResource(R.mipmap.red_big);
//                            areaCircle.setBackground(Color.parseColor("#df5f5f"));
                        } else if (Common.xglhcColor.getGreen().contains(temp[t])) {
//                            areaCircle.setBackground(Color.parseColor("#85ba75"));
                            areaCircle.setBackgroundResource(R.mipmap.green_big);
                        } else {
//                            areaCircle.setBackground(Color.parseColor("#7b95e0"));
                            areaCircle.setBackgroundResource(R.mipmap.blue_big);
                        }
                        areaCircle.setText(temp[t]);
                        areaCircle.setGravity(Gravity.CENTER);
                        areaCircle.setTextColor(Color.parseColor("#3f3f3f"));
                        areaCircle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        layoutParams.rightMargin = Utils.dip2px(mContext, 6);
                        areaCircle.setLayoutParams(layoutParams);
                        container.addView(areaCircle);
                    }
                }
            } else if("ahk3".equals(data.getCode())||"jiangsuk3".equals(data.getCode())||Common.isK3(data.getCode())){
                // 快3的图片显示
                for (String s : temp) {
                    int num = Integer.parseInt(s);
                    ImageView imageView = new ImageView(mContext);
                    imageView.setImageResource(Common.getDiceDrawable(num));
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 20), Utils.dip2px(mContext, 20));
                    layoutParams.rightMargin = Utils.dip2px(mContext, 6);
                    imageView.setLayoutParams(layoutParams);
                    container.addView(imageView);
                }
            }else {
                for (String s : temp) {
                    AreaCircle areaCircle = new AreaCircle(mContext);
                    Common.setBallBackground(areaCircle, s, data.getCode());
                    areaCircle.setText(s);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 20), Utils.dip2px(mContext, 20));
                    layoutParams.rightMargin = Utils.dip2px(mContext, 6);
                    areaCircle.setLayoutParams(layoutParams);
                    container.addView(areaCircle);
                }
            }
            container.setVisibility(View.VISIBLE);
//            tip.setVisibility(VISIBLE);
        } else {
            number.setText(data.getPrize_num());
            number.setVisibility(View.VISIBLE);
            container.setVisibility(View.GONE);
//            tip.setVisibility(GONE);
        }

        tip.setText("第" + data.getNo() + "期");

        time.setText(data.getDate());

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (data.getCode().equals("xglhc") || data.getCode().equals("jslhc")|| data.getCode().equals("js10flhc")) {
                    Intent intent = new Intent(mContext, LiuhecaiPrizeActivity.class);
                    intent.putExtra("code", data.getCode());
                    mContext.startActivity(intent);
                } else {
                    Intent intent = new Intent(mContext, PrizeListActivity.class);
                    intent.putExtra("code", data.getCode());
                    mContext.startActivity(intent);
                }
            }
        });
    }
}
